package com.qst.controller;

import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.RequestUtil;
import com.qst.entity.AssessmentType;
import com.qst.service.AssessmentService;
import com.qst.service.DimensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/assessment")
@SuppressWarnings("all")
public class AssessmentTypeController extends BaseServlet {

    @Autowired
    @Qualifier("assessmentServiceSsmImpl")
    AssessmentService assessmentService;

    @Autowired
    @Qualifier("dimensionServiceSsmImpl")
    DimensionService dimensionService;


    @RequestMapping("/view.action")
    public String view(HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
       //查看考核类型
        int id = RequestUtil.getInt(req, "id");
        AssessmentType assessment = assessmentService.findAssessmentById(id);
        req.setAttribute("assessment", assessment);
//        WebUtil.forward(req, resp, "/assessment/view.jsp");
        return "/assessment/view";

   }

   @RequestMapping("/update.action")
   public String update(HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
       AssessmentType sj = new AssessmentType();
       sj.setId(RequestUtil.getInt(req, "id"));
       sj.setTitle(RequestUtil.getString(req, "title"));
       sj.setCost(RequestUtil.getDouble(req, "cost"));
       sj.setStatus(RequestUtil.getInt(req, "status"));
       try {
           assessmentService.updateAssessment(sj);
           addMessage(req, "考核类型信息已被保存到数据库");
//           WebUtil.redirect(req, resp, "/assessment/view.action?id=" + sj.getId());
           return "redirect:/assessment/view.action?id=" + sj.getId();
       } catch (ExamException ex) {
           addError(req, ex.getMessage());
           req.setAttribute("assessment", sj);
//           WebUtil.forward(req, resp, "/assessment/edit.jsp");
           return "/assessment/edit";
       }

   }

    @RequestMapping("/save.action")
    public String save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AssessmentType sj = new AssessmentType();
        sj.setTitle(RequestUtil.getString(req, "title"));
        sj.setCost(RequestUtil.getDouble(req, "cost"));
        sj.setStatus(RequestUtil.getInt(req, "status"));
        try {
            assessmentService.saveAssessment(sj);
            addMessage(req, "考核类型信息已被保存到数据库");
//            WebUtil.redirect(req, resp, "/assessment/view.action?id=" + sj.getId());
            return "redirect:/assessment/view.action?id=" + sj.getId();
        } catch (ExamException ex) {
            addError(req, ex.getMessage());
            req.setAttribute("assessment", sj);
//            WebUtil.forward(req, resp, "/assessment/create.jsp");
            return "/assessment/create";
        }
    }

   @RequestMapping("list.action")
    public String list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
       req.setAttribute("assessmentList", assessmentList);
//       WebUtil.forward(req, resp, "/assessment/list.jsp");
       return "/assessment/list";
   }


   @RequestMapping("/edit.action")
    public String edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       int id = RequestUtil.getInt(req, "id");
       AssessmentType assessment = assessmentService.findAssessmentById(id);
       req.setAttribute("assessment", assessment);
//       WebUtil.forward(req, resp, "/assessment/edit.jsp");
       return "/assessment/edit";
   }

   @RequestMapping("/delete.action")
   public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       int id = RequestUtil.getInt(req, "id");
         //bug 点 考核类型在删除前应该没有绑定的性格维度
       if (dimensionService.findDimensionByAssessment(id).size() > 0) {
           addMessage(req, "考核类型已绑定性格维度，无法删除");
//           WebUtil.redirect(req, resp, "/assessment/list.action");
           return "redirect:/assessment/list.action";
       }
       try {
           assessmentService.deleteAssessment(id);
           addMessage(req, "考核类型已删除");
       } catch (ExamException ex) {
           addError(req, ex.getMessage());
       }
//       WebUtil.redirect(req, resp, "/assessment/list.action");
       return "redirect:/assessment/list.action";
   }
   @RequestMapping("/create.action")
   public String create(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       WebUtil.forward(req, resp, "/assessment/create.jsp");
       return "/assessment/create";
   }
}
